import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./routes.js";

Vue.use(VueRouter);

const orginPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return orginPush.call(this,location).catch(err=>err);
};
const router = new VueRouter({
  routes
});

router.beforeEach((to,from,next)=>{
  console.log(to,from);
  if(to.meta.login) {
      if(to.path === '/login') {
        next();
      }
      if(!localStorage.getItem('user')) {
          next({path:'/login'});
      }
  }
  next();
});

export default router;
